<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="90%">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="98px">
      <el-form-item label="产品编码" prop="goodsCode">
        <el-input v-model="queryParams.goodsCode" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品型号" prop="goodModel">
        <el-input v-model="queryParams.goodModel" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-form ref="form" :model="form" :rules="rules">
      <el-table show-summary :summary-method="getSummaries" :data="columns" ref="dragTable" row-key="id" v-loading="loading"
        @sort-change="handleSortChange" @selection-change="handleSelectionChange" :max-height="tableMaxHeight">
        <el-table-column align="center" label="序号" type="index" width="50">
          <template slot-scope="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <!--  <el-table-column sortable align="center" label="采购单号" prop="goodsCode" />-->
        <el-table-column sortable align="center" label="采购单号" prop="caigouno">
          <template slot-scope="scope">
            <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
              <span @click="caigouDeatil(scope.row)" style="color: #00afff;cursor:pointer;">
                {{ scope.row.caigouno }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column sortable align="center" label="产品编码" prop="goodsCode" />
        <el-table-column sortable align="center" label="产品名称" prop="goodsName"/>
        <el-table-column sortable align="center" label="产品型号" prop="goodModel" />
        <el-table-column sortable align="center" label="订单数量" prop="orderCount" />
        <el-table-column sortable align="center" label="请购数量" prop="pleaseBuyCount" />
        <el-table-column sortable align="center" label="采购单价" prop="goodsBuyPrice" />
        <el-table-column sortable align="center" label="采购数量" prop="shengyu" />
        <el-table-column sortable align="center" label="采购总价" prop="goodsBuyTotal" />
        <el-table-column sortable label="产品状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.goods_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column sortable align="center" label="备注" prop="remark" />
      </el-table>
    </el-form>
    <div class="dialog-footer" slot="footer">
      <el-button @click="cancel">关 闭</el-button>
    </div>
    <PurchaseDetailsFormList ref="PurchaseDetailsFormList"></PurchaseDetailsFormList>
    <choseGoods-form-list @ok="getList" ref="choseGoodsFormList"></choseGoods-form-list>
    <choseGoodsDeatil-form-list ref="choseGoodsDeatilFormList" @ok="getList"></choseGoodsDeatil-form-list>
  </el-dialog>
</template>

<script>
import { addAction, isEmpty, deleteAction, getAction, getById, accAdd } from '@/api/manage'
import PurchaseDetailsFormList from '@/views/develop/pleaseBuy/module/PurchaseDetailsFormList'
import ChoseGoodsFormList from '@/views/develop/goodsSaleMaterial/module/ChoseGoodsFormList'
import ChoseGoodsDeatilFormList from '@/views/develop/goodsSaleMaterial/module/ChoseGoodsDeatilFormList'

export default {
  name: "CaigouDetailForm",
  dicts: ['goods_status', 'isdo'],
  components: {
    ChoseGoodsFormList,
    ChoseGoodsDeatilFormList,
    PurchaseDetailsFormList
  },
  data() {
    return {
      tableMaxHeight: window.innerHeight - 250,
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "采购订单产品请购",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      proId: "",
      status: "",
      columns: [],
      total: 0,
      // 表单校验
      rules: {
      },
      queryParams: {
        pageNum: 1,
        pageSize: 1000,
        id: null,
        goodsName: null,
        goodsCode: null,
        goodModel: null,
        qinggouId: null,
      },
      url: {
        list: '/goodsSalebuy/goodsSalebuy/listByQinggou',
      },
      selectedRows: [], // 用于存储勾选的行数据
    };
  },
  updated() {
    this.$nextTick(() => {
      // tab 绑定ref 属性
      // this.$refs["dragTable"].doLayout();
    });
  },
  created() {

  },
  methods: {
    caigouDeatil(row) {
      this.$refs.PurchaseDetailsFormList.openDialog(row);
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    show(row) {
      this.open = true;
      this.title = "采购订单产品请购";
      this.queryParams.qinggouId = row.id;
      this.getList()
    },
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.columns = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        name: null,
        master: null,
        customId: null,
        customName: null,
        companyName: null,
        contractDate: null,
        contractMoney: null,
        auditMoney: null,
        checkDate: null,
        checkState: null,
        fundCallback: null,
        infomation: null,
        shengyu: null,
      };
      this.resetForm("form");
    },
    handleSelectionChange(selection) {
      this.selectedRows = selection;
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "orderCount" || column.property === "pleaseBuyCount" || column.property === "goodsBuyPrice" || column.property === "shengyu" || column.property === "goodsBuyTotal") {
          sums[index] = values.reduce((prev, curr) => {

            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
  },
};
</script>
<style></style>
